﻿Public Class frmQuanLyThucDon
    Shared thucDon As New CThucDon
    Shared da As New DataProvider
    Private Sub frmQuanLy_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dgvDsThucDon.DataSource = thucDon.LoadDsThucDon()
        LoadCbxLoaiMonAn()
        LoadCbxLoaiDonVi()
    End Sub
    Public Sub LoadCbxLoaiDonVi()
        Dim Sql = "select * from DONVI"
        cbxDonVi.DataSource = CKiemTra.LoadDS(Sql)
        cbxDonVi.DisplayMember = "TenDV"
        cbxDonVi.ValueMember = "MaDV"
    End Sub
    Public Sub LoadCbxLoaiMonAn()
        Dim Sql = "select * from LOAIMONAN"
        cbxLoaiSP.DataSource = CKiemTra.LoadDS(Sql)
        cbxLoaiSP.DisplayMember = "TenLoaiMonAn"
        cbxLoaiSP.ValueMember = "MaLoaiMonAn"
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        thucDon.TenMonAn = txtTenSP.Text
        'thucDon.MaMonAn = Integer.Parse(txtMaSP.Text)
        thucDon.MaLoaiMonAn = cbxLoaiSP.SelectedValue
        thucDon.DonGia = Integer.Parse(txtDonGia.Text)
        thucDon.MaDonVi = cbxDonVi.SelectedValue
        CThucDon.InsertThucDon(thucDon)
        dgvDsThucDon.DataSource = thucDon.LoadDsThucDon()
    End Sub

    Private Sub dgvDsThucDon_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDsThucDon.SelectionChanged
        Dim index = dgvDsThucDon.CurrentRow.Index
        Dim row = dgvDsThucDon.Rows.Item(index)
        txtMaSP.Text = row.Cells(0).Value.ToString()
        txtTenSP.Text = row.Cells(1).Value.ToString()
        cbxLoaiSP.Text = row.Cells(2).Value.ToString() ' ten mon an


        cbxDonVi.Text = row.Cells(3).Value.ToString() 'ten don vi
        txtDonGia.Text = row.Cells(4).Value.ToString() ' don gia
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim index = dgvDsThucDon.CurrentRow.Index
        CThucDon.DeleteThucDon(dgvDsThucDon.CurrentRow.Cells(0).Value)
        dgvDsThucDon.Rows.RemoveAt(index)
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        thucDon.MaMonAn = Integer.Parse(txtMaSP.Text)
        thucDon.TenMonAn = txtTenSP.Text
        thucDon.MaLoaiMonAn = cbxLoaiSP.SelectedValue
        thucDon.MaDonVi = cbxDonVi.SelectedValue
        thucDon.DonGia = Integer.Parse(txtDonGia.Text)

        CThucDon.UpdateThucDon(thucDon)

        dgvDsThucDon.DataSource = thucDon.LoadDsThucDon()
    End Sub
End Class